//
//  GYVisitorViewController.m
//  Visitor
//
//  Created by x on 2017/9/12.
//  Copyright © 2017年 HLB. All rights reserved.
//

#import "GYVisitorViewController.h"
#import "GYVisitorTableViewCell.h"
#import "GYVisitorDetailsViewController.h"
#import "GYMessageTableViewController.h"
#import "GYNetWorkTools.h"

#import "GYVisitor.h"
#import "NSString+FilePath.h"

static NSString *visitorCellIDIdentifier = @"visitorCellIDIdentifier";

@interface GYVisitorViewController ()<UITableViewDelegate,UITableViewDataSource>

/** 访客展示 */
@property (nonatomic, weak) UITableView *visitoTableView;

/** 访客表单 */
@property (nonatomic, strong) NSMutableArray *visitorList;
@end

@implementation GYVisitorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //0.监听推送通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateVisitorListInfo) name:@"updateVisitorInfo" object:nil];
    
    //1.初始化
    [self setupUI];
    
    //2.初始化表格
    [self setupTableView];
}

- (void)setupUI {
    self.view.backgroundColor = RGBColor(0XF0F0F0);
    self.navigationItem.title = @"访客通知";
    //self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithBack:@"my_icon_news" HightImage:nil Target:self action:@selector(newsBtuClick)];
    
    //1.加载访客数据
    [self loadVisitorData];
}

- (void)setupTableView {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.rowHeight = 72;
    //注册cell
    [tableView registerClass:[GYVisitorTableViewCell class] forCellReuseIdentifier:visitorCellIDIdentifier];

    [self.view addSubview:tableView];
    
    self.visitoTableView = tableView;
}

- (void)newsBtuClick {
    //跳转消息界面
    GYMessageTableViewController *messageController = [[GYMessageTableViewController alloc] init];
    [self.navigationController pushViewController:messageController animated:YES];
}

/**
 刷新访客记录
 */
- (void)updateVisitorListInfo {
    NSLog(@"收到通知有没有走这个方法...");
    //刷新访客记录
    [self loadVisitorData];

}

/**
 刷新访客记录
 */
- (void)loadVisitorData {
    //NSLog(@"执行刷新访客记录的请求....");
    [GYVisitor loadVisitorData:^(NSMutableArray *array) {
        //1.拿到数组之后要进行排序
        
        self.visitorList = [[array sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            GYVisitor *model1 = (GYVisitor *)obj1;
            GYVisitor *model2 = (GYVisitor *)obj2;
            
            return [model2.visitorId compare:model1.visitorId];
        }] mutableCopy];
        [self.visitoTableView reloadData];
    }];
}

#pragma mark -- <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.visitorList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYVisitorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:visitorCellIDIdentifier];
    cell.model = self.visitorList[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    GYVisitorDetailsViewController *visitorDetails = [[GYVisitorDetailsViewController alloc] init];
    visitorDetails.model = self.visitorList[indexPath.row];
    visitorDetails.index = indexPath.row;
    visitorDetails.visitorBlock = ^(NSInteger currentIndex, int handleState) {
        GYVisitor *model = self.visitorList[currentIndex];
        model.result = handleState;
        [self.visitoTableView reloadData];
    };
    [self.navigationController pushViewController:visitorDetails animated:YES];
}

#pragma mark -- 懒加载

- (NSMutableArray *)visitorList
{
    if (!_visitorList) {
        _visitorList = [NSMutableArray array];
    }
    return _visitorList;
}

@end
